﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.Models;
using PNProject.EF;

namespace PNProject.Controllers
{
    public class AdminCategoryController : Controller
    {
        //
        // GET: /AdminCategory/

        public ActionResult Index()
        {
            var list = StaticData.cateRepo.GetAll().ToList();
            ViewData["model"] = list;
            return View();
        }

        public ActionResult Create()
        {
            return View(new Category());
        }

        [HttpPost]
        public ActionResult Create(Category model)
        {
            if (!string.IsNullOrEmpty(model.CategoryName) && !string.IsNullOrEmpty(model.CategoryNameEN))
            {
                model.TLink = PNLibraries.PNChuyenKhongDau(model.CategoryName);
                model.LastUpdate = DateTime.Now;
                model.TLinkEN = PNLibraries.PNChuyenKhongDau(model.CategoryNameEN);
                StaticData.cateRepo.Save(null, model);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Edit(string id)
        {
            try
            {
                int newid = int.Parse(id);
                var dbo = StaticData.cateRepo.GetOne(a => a.CategoryID == newid);
                return View(dbo);
            }
            catch (Exception e)
            {
                return RedirectToAction("Error", "Admin");
            }
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Category model)
        {
            var dbo = StaticData.cateRepo.GetOne(a => a.CategoryID == model.CategoryID);
            if (dbo != null)
            {
                model.TLink = PNLibraries.PNChuyenKhongDau(model.CategoryName);
                model.TLinkEN = PNLibraries.PNChuyenKhongDau(model.CategoryNameEN);
                model.LastUpdate = DateTime.Now;
                StaticData.cateRepo.Save(dbo, model);
            }
            return View();
        }

        public ActionResult Delete(string id)
        {
            try
            {
                int newid = int.Parse(id);
                var check = StaticData.proRepo.GetSome(a => a.CategoryID == newid).FirstOrDefault();
                if (check == null)
                {
                    var dbo = StaticData.cateRepo.GetOne(a => a.CategoryID == newid);
                    if (dbo != null)
                    {
                        StaticData.cateRepo.Delete(dbo);
                        return RedirectToAction("Index");
                    }
                    else
                        return RedirectToAction("Error", "Admin");
                }
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return RedirectToAction("Error", "Admin");
            }
            return View();
        }

    }
}
